package com.cjt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjt.common.dto.TeaStuDto;
import com.cjt.common.dto.TeaSubjectDto;
import com.cjt.common.dto.TeaSubjectListDto;
import com.cjt.common.vo.TeaEditScoreVo;
import com.cjt.mapper.SubjetMapper;
import com.cjt.pojo.*;
import com.cjt.service.*;
import com.cjt.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author Hello
* @description 针对表【teacher】的数据库操作Service实现
* @createDate 2023-03-06 21:47:47
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{

    @Autowired
    GradeStuService gradeStuService;
    @Autowired
    StudentService studentService;
    @Autowired
    SubjetMapper subjetMapper;
    @Autowired
    ScoreService scoreService;
    @Autowired
    Sub_ScoreService subScoreService;
    @Autowired
    Stu_ScoreService stu_scoreService;
    @Autowired
    SubjetService subjetService;
    @Autowired
    GradeService gradeService;

    @Override
    public List<TeaStuDto> getStus() {
        TeaStuDto teaStuDto = new TeaStuDto();
        List<TeaStuDto> listDto = new ArrayList<>();
        //获取职工号
        String userid = SecurityContextHolder.getContext().getAuthentication().getName();
        //通过老师职工号在老师表中找到所教班级
        Teacher teacher = this.getOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeaId, userid));
        Grade one = gradeService.getOne(new LambdaQueryWrapper<Grade>().eq(Grade::getId, teacher.getTeaclass()));
        //通过所教班级号在 班级学生表中找到对应的 学生id
        GradeStu g = gradeStuService.getOne(new LambdaQueryWrapper<GradeStu>().eq(GradeStu::getGradeId, teacher.getTeaclass()));
        //通过学生id在学生表中找到对应信息
        List<Student> list = studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getStuId, g.getStuId()));
        for (Student student : list) {
            teaStuDto.setStuId(student.getStuId());
            teaStuDto.setAvatar(student.getAvatar());
            teaStuDto.setBirthday(student.getBirthday());
            teaStuDto.setSex(student.getSex());
            teaStuDto.setName(student.getName());
            teaStuDto.setClazzName(one.getName());
            teaStuDto.setPhone(student.getPhone());
            TeaStuDto new1 = BeanUtil.copyProperties(teaStuDto, TeaStuDto.class);
            listDto.add(new1);
        }
        return listDto;
    }

    @Override
    public List<TeaSubjectDto> listSubject(Long id) {
        String userid = SecurityContextHolder.getContext().getAuthentication().getName();
        TeaSubjectDto teaSubjectDto = new TeaSubjectDto();
        List<TeaSubjectDto> list = new ArrayList<>();
        Subjet s = subjetService.getById(id);
        //通过科目id在 科目成绩表 中找到对应的数据
        List<Sub_Score> list1 = subScoreService.list(new LambdaQueryWrapper<Sub_Score>().eq(Sub_Score::getSubId, s.getId()));
        for (Sub_Score sub_score : list1) {
            Score score = scoreService.getOne(new LambdaQueryWrapper<Score>().eq(Score::getId, sub_score.getScoreId()));
            Stu_Score stuScore = stu_scoreService.getOne(new LambdaQueryWrapper<Stu_Score>().eq(Stu_Score::getScoreId, sub_score.getSubId()));
            Student student = studentService.getOne(new LambdaQueryWrapper<Student>().eq(Student::getStuId, stuScore.getStuId()));
            teaSubjectDto.setSubId(s.getId());
            teaSubjectDto.setStatus(score.getStatu());
            teaSubjectDto.setStuName(student.getName());
            teaSubjectDto.setSubName(s.getName());
            teaSubjectDto.setScoreId(score.getId());
            teaSubjectDto.setUpdated(score.getUpdated());
            teaSubjectDto.setScore(score.getScore());
            TeaSubjectDto news = BeanUtil.copyProperties(teaSubjectDto, TeaSubjectDto.class);
            list.add(news);
        }

        return list;
    }

    @Override
    public void editScore(TeaEditScoreVo teaEditScoreVo) {
        List<Long> subid = getCheakSubid();
        Long scoreId = teaEditScoreVo.getScoreId();
        if (!subid.contains(scoreId)) {
            throw new RuntimeException("系统异常");
        }
        Score score = new Score();
        score.setStatu(1);
        score.setUpdated(new Date());
        score.setScore(teaEditScoreVo.getScore());
        score.setId(teaEditScoreVo.getScoreId());
        scoreService.updateById(score);
    }

    //获取老师所教科目id
    public List<Long> getCheakSubid() {
        String userid = SecurityContextHolder.getContext().getAuthentication().getName();
        List<Subjet> subjetList = subjetMapper.getByTeaId(Long.valueOf(userid));
        List<Long> ids = new ArrayList<>();
        for (Subjet s : subjetList) {
            Long id = s.getId();
            ids.add(id);
        }
        return ids;
    }


    public List<TeaSubjectListDto> getTeacherSubList() {
        List<Long> cheakSubid = getCheakSubid();
        TeaSubjectListDto teaSubjectListDto = new TeaSubjectListDto();
        List<TeaSubjectListDto> list = new ArrayList<>();
        for (Long s : cheakSubid) {
            teaSubjectListDto.setId(s);
            Subjet byId = subjetService.getById(s);
            teaSubjectListDto.setValue(byId.getName());
            TeaSubjectListDto news = BeanUtil.copyProperties(teaSubjectListDto, TeaSubjectListDto.class);
            list.add(news);
        }
        return list;
    }

}




